<?php
namespace App\Http\Controllers\MobileWeb;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\App\QrCode\QrCode;
use Illuminate\Support\Facades\Auth;

class QrCodeController extends WebController
{
    //商家的二维码
    public function shopQrCode($id) {
        $userAgent = $this->request->header('user-agent');
        if(strstr($userAgent, "FBWiOSApp")) {
            $platform = "iOS";
        } elseif(strstr($userAgent, "FBWAndroidApp")) {
            $platform = "Android";
        } elseif(stripos($userAgent, 'Android')) {
            $platform = 'Android';
        } elseif(stripos($userAgent, "iphone")) {
            $platform = "iOS";
        } elseif(stripos($userAgent, "ipad")) {
            $platform = "iOS";
        } else {
            $platform = "Android";
        }
        if(stripos($userAgent, 'MicroMessenger')) {
            $isWeiXin = '1';
        } else {
            $isWeiXin = '0';
        }
        if($userAgent == 'FBWiOSApp' || $userAgent == 'FBWAndroidApp') {
            //手机app扫描过来的
            return $this->responseSuccessMessage("参数正确");
        } else {
            //不是从手机app扫描过来的,从手机浏览器或者微信等
            $result = QrCode::shopQrCode($id);
            if($result['status'] == 'failed') {
                return view('errors.404');
            }
            $data = $result['data'];
            if($data['type'] == 'downloadapp') {
                return redirect()->to('/mobile/app/download');
            } elseif($data['type'] == 'shopsalesman') {
                $shopName = $data['shopname'];
                $shopId = $data['shopid'];
                if($this->request->has('shopuserid')) {
                    $shopUserId = $this->request->get('shopuserid');
                } else {
                    $shopUserId = '';
                }
                $data = [
                    'platform' => $platform,
                    'isweixin' => $isWeiXin,
                    'shopid' => $shopId,
                    'shopuserid' => $shopUserId,
                    'shopname' => urldecode($shopName),
                ];
                return view('mobile.salesman.gotodown_app', $data);
            } else {
                return view('errors.404');
            }
        }
    }
    public function h5QrCode($id) {
        return redirect()->to('/mobile/app/download');
    }
    //用户版二维码
    public function userQrCode($userId) {
        $userAgent = $this->request->header('user-agent');
        if($userAgent == 'FBWiOSApp' || $userAgent == 'FBWAndroidApp') {
            //手机app扫描过来的
            return $this->responseSuccessMessage("参数正确");
        } else {
            //不是从手机app扫描过来的,从手机浏览器或者微信等
            $result = QrCode::userQrCode($userId);
            if ($result['status'] == 'failed') {
                return view('errors.404');
            }
            $data = $result['data'];
            if ($data['type'] == 'downloadapp') {
                return redirect()->to('/mobile/app/download');
            } elseif ($data['type'] == 'usersalesman') {
                $nickName = $data['nickname'];
                $mobile = $data['mobile'];
                $userId = $data['userid'];
                $data = [
                    'userid' => $userId,
                    'mobile' => $mobile,
                    'nickname' => urldecode($nickName),
                ];
                return view('mobile.salesman.user_salesman_download_app', $data);
            } else {
                return view('errors.404');
            }
        }
    }
}